# 导入相关库
import pandas as pd
import numpy as np
from nni.algorithms.feature_engineering.gbdt_selector import GBDTSelector
cat = {
    'model': 'category',
    'brand': 'category',
    'bodyType': 'category',
    'fuelType': 'category',
    'gearbox': 'category',
    'notRepairedDamage': 'category',
    'regionCode': 'category',
}

df = pd.read_csv('./user_data/df.csv', sep=' ', dtype=cat)

for i in range(15):
    for j in range(i+1, 15):
        df['new'+str(i)+'*'+str(j)] = df['v_'+str(i)]*df['v_'+str(j)]


#第二批特征工程
for i in range(15):
    for j in range(i+1, 15):
        df['new'+str(i)+'+'+str(j)] = df['v_'+str(i)]+df['v_'+str(j)]

# 第三批特征工程
for i in range(15):
    df['new' + str(i) + '*power'] = df['v_' + str(i)] * df['power']


#第四批特征工程
for i in range(15):
    for j in range(i+1, 15):
        df['new'+str(i)+'-'+str(j)] = df['v_'+str(i)]-df['v_'+str(j)]


"""
筛选特征
"""
numerical_cols = df.select_dtypes(exclude='object').columns

# initlize a selector
fgs = GBDTSelector()
# fit data
param = {'boosting_type': 'gbdt',
         'num_leaves': 55,  # 过小容易过拟合，过大则计算时间长
         'max_depth': 35,
         "lambda_l2": 2,  # 防止过拟合
         'min_data_in_leaf': 20,  # 防止过拟合，好像都不用怎么调
         'objective': 'regression_l1',
         'learning_rate': 0.02,
         "min_child_samples": 20,
         "feature_fraction": 0.7,
         "bagging_freq": 1,
         "bagging_fraction": 0.9,
         "bagging_seed": 11,
         "metric": 'mae',
         }
train_X = df[df.train == 1].drop(['price', 'SaleID', 'regionCode'], axis=1)
train_y = df[df.train == 1]['price']
train_y_ln = np.log(train_y)
fgs.fit(train_X, train_y_ln, lgb_params=param, eval_ratio=0.2,
        early_stopping_rounds=300, importance_type='gain',
        num_boost_round=15000
        )
gain = pd.DataFrame(fgs.feature_importance)
gain['name'] = train_X.columns
gain = gain.sort_values(0, ascending=False)
df[list(gain.iloc[:60].name)+['price', 'SaleID', 'regionCode', 'train']
   ].to_csv('./user_data/df_s.csv', index=0, sep=' ')
